package com.shopping.www.service;

import com.shopping.www.mapper.BrandMapper;
import com.shopping.www.model.Brand;
import com.shopping.www.model.BrandAndNum;
import com.shopping.www.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class BrandService {
    SqlSessionFactory factory= SqlSessionFactoryUtils.getSqlSessionFactory();
    private AdminService service2=new AdminService();
    public List<Brand> selectAllBrand(Brand brand){
        SqlSession sqlSession=factory.openSession();
        BrandMapper mapper =sqlSession.getMapper(BrandMapper.class);
        String brandName=brand.getBrandName();
        if(brandName!=null&&brandName.length()>0){
            brand.setBrandName("%"+brandName+"%");
        }
        String BusinessName=brand.getBusinessName();
        if(BusinessName!=null&&BusinessName.length()>0){
            brand.setBusinessName("%"+BusinessName+"%");
        }
        int[] ids=mapper.selectIds();
        if(ids.length>0){
            List<Brand> brands=mapper.selectAllBrand(ids,brand);
            sqlSession.close();
            return brands;
        }
        else{
            List<Brand> brandList=new ArrayList<>();
            return brandList;
        }
    }
    public int selectBusinessNum(Integer id){
        SqlSession sqlSession=factory.openSession();
        BrandMapper mapper =sqlSession.getMapper(BrandMapper.class);
        int shelveNum= mapper.selectBusinessNum(id);
        sqlSession.close();
        return shelveNum;
    }
    public void addToCart(int brand_id,int user_id,int cartNum){
        SqlSession sqlSession=factory.openSession();
        BrandMapper mapper =sqlSession.getMapper(BrandMapper.class);
        Integer checkCart=mapper.checkCart(brand_id,user_id);
//        System.out.println(checkCart);
        if(checkCart==null){
            mapper.addToCart(brand_id,user_id,cartNum);
        }else {
            mapper.updateCart(checkCart,cartNum);
        }
        sqlSession.commit();
        sqlSession.close();
    }
    public List<Brand> selectAllSortBrand(Brand brand,String belong){
        SqlSession sqlSession=factory.openSession();
        BrandMapper mapper =sqlSession.getMapper(BrandMapper.class);
        String brandName=brand.getBrandName();
        if(brandName!=null&&brandName.length()>0){
            brand.setBrandName("%"+brandName+"%");
        }
        String BusinessName=brand.getBusinessName();
        if(BusinessName!=null&&BusinessName.length()>0){
            brand.setBusinessName("%"+BusinessName+"%");
        }
        int flag=0;
        int[] ids1=mapper.selectSortIds(belong);
        int[] ids2=mapper.selectIds();
        int[] ids = new int[ids1.length>ids2.length?ids1.length:ids2.length];
        for (int i = 0; i < ids1.length; i++) {
            for(int j=0;j<ids2.length;j++){
                if(ids1[i]==ids2[j]){
                    ids[flag]=ids2[j];
                    flag++;
                }
            }
        }
        if(ids.length>0){
            List<Brand> brands=mapper.selectAllBrand(ids,brand);
            sqlSession.close();
            return brands;
        }
        else{
            List<Brand> brandList=new ArrayList<>();
            return brandList;
        }
    }
    public BrandAndNum<Brand> selectCartBrands(int user_id) throws UnsupportedEncodingException{
        SqlSession sqlSession=factory.openSession();
        BrandMapper mapper =sqlSession.getMapper(BrandMapper.class);
        int[] ids=mapper.selectCartIds(user_id);
        int[] nums=mapper.selectCartNums(user_id);
        BrandAndNum<Brand> brandAndNum=new BrandAndNum<>();
        if(ids.length>0) {
            List<Brand> brands=mapper.selectBrands(ids);
            List<Brand> brandList=new ArrayList<>();
            for (int i = 0; i < ids.length; i++) {
                for(int j=0;j<brands.size();j++) {
                    if (brands.get(j).getId() == ids[i]) {
                        brandList.add(brands.get(j));
                    }
                }
            }
            brandAndNum.setBrands(brandList);
            brandAndNum.setIds(ids);
            brandAndNum.setNums(nums);
            sqlSession.close();
            return brandAndNum;
        }else{
            sqlSession.close();
            return brandAndNum;
        }
    }
    public int[] selectCartBrandMaxNum(int user_id){
        SqlSession sqlSession = factory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        int[] ids=mapper.selectCartIds(user_id);
        if(ids.length>0) {
            int[] maxNum = mapper.selectCartBrandMaxNum(ids);
        }
        else{
            int[] maxNum=new int[0];
        }
        int[] maxNum=mapper.selectCartBrandMaxNum(ids);
        return maxNum;
    }
    public int checkCartBrandStatus(int brand_id,int user_id){
        SqlSession sqlSession = factory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        int status= mapper.checkCartBrandStatus(brand_id);
        return status;
    }
    public void updateCartNum(int brand_id,int user_id,int num){
        SqlSession sqlSession = factory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        mapper.updateCartNum1(brand_id,user_id,num);
        sqlSession.commit();
        sqlSession.close();
    }
    public void deleteCartByIds(int[] ids,int user_id){
        SqlSession sqlSession = factory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        mapper.deleteCartByIds(ids,user_id);
        sqlSession.commit();
        sqlSession.close();
    }
    public int addToOrder(int brand_id,int user_id,int orderNum,int flag) {
        SqlSession sqlSession = factory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        int shelveNum = mapper.selectBusinessNum(brand_id);
        if(shelveNum!=0){
        if(shelveNum>=orderNum) {
            mapper.minxShelveNum(brand_id, shelveNum, orderNum);
            if (flag == 1) {
                mapper.updateCartNum(brand_id, orderNum);
            } else {
                mapper.addToOrder(brand_id, user_id, orderNum);
            }
            sqlSession.commit();
            sqlSession.close();
            return 1;
        }else{
            return 2;
        }
        }else{
            return 3;
        }
    }
    public BrandAndNum<Brand> selectOrderBrands(int user_id) throws UnsupportedEncodingException {
        SqlSession sqlSession=factory.openSession();
        BrandMapper mapper =sqlSession.getMapper(BrandMapper.class);
        int[] ids=mapper.selectOrderIds(user_id);
        int[] nums=mapper.selectOrderNums(user_id);
        BrandAndNum<Brand> brandAndNum=new BrandAndNum<>();
        if(ids.length>0) {
            List<Brand> brands=mapper.selectBrands(ids);
            List<Brand> brandList=new ArrayList<>();
            for (int i = 0; i < ids.length; i++) {
                for(int j=0;j<brands.size();j++) {
                    if (brands.get(j).getId() == ids[i]) {
                        brandList.add(brands.get(j));
                    }
                }
            }
//            for(Brand brand:brandList){
//                String encode= URLEncoder.encode(brand.getPhoto(),"utf-8");
//                System.out.println(encode);
//                String decode= URLDecoder.decode(encode,"utf-8");
//                System.out.println(decode);
//            }
//            System.out.println(brandList);
            brandAndNum.setBrands(brandList);
            brandAndNum.setIds(ids);
            brandAndNum.setNums(nums);
            sqlSession.close();
            return brandAndNum;
        }else{
            sqlSession.close();
            return brandAndNum;
        }
    }
    public void deleteOrder(int user_id,int flag){
        SqlSession sqlSession=factory.openSession();
        BrandMapper mapper =sqlSession.getMapper(BrandMapper.class);
        int[] ids=mapper.selectDeleteOrder(user_id);
        mapper.deleteOrder(ids[flag]);
        sqlSession.commit();
        sqlSession.close();
    }
    public void chooseDelivery(int user_id,int brand_id,int maxNum,int num){
        SqlSession sqlSession=factory.openSession();
        BrandMapper mapper =sqlSession.getMapper(BrandMapper.class);
        mapper.chooseDelivery1(user_id,brand_id);
        mapper.chooseDelivery2(brand_id,maxNum,num);
        sqlSession.commit();
        sqlSession.close();
    }
    public BrandAndNum<Brand> selectUserDelivery(int user_id) throws UnsupportedEncodingException{
        SqlSession sqlSession=factory.openSession();
        BrandMapper mapper =sqlSession.getMapper(BrandMapper.class);
        int[] ids=mapper.selectDeliveryIds(user_id);
        int[] status=mapper.selectDeliveryStatus(user_id);
        int[] nums=mapper.selectCartNums(user_id);
        BrandAndNum<Brand> brandAndNum=new BrandAndNum<>();
        if(ids.length>0) {
            List<Brand> brands=mapper.selectBrands(ids);
            List<Brand> brandList=new ArrayList<>();
            for (int i = 0; i < ids.length; i++) {
                for(int j=0;j<brands.size();j++) {
                    if (brands.get(j).getId() == ids[i]) {
                        brandList.add(brands.get(j));
                    }
                }
            }
            brandAndNum.setBrands(brandList);
            brandAndNum.setIds(status);
            brandAndNum.setNums(nums);
            sqlSession.close();
            return brandAndNum;
        }else{
            sqlSession.close();
            return brandAndNum;
        }
    }
    public void finishDelivery(int user_id,int brand_id){
        SqlSession sqlSession=factory.openSession();
        BrandMapper mapper =sqlSession.getMapper(BrandMapper.class);
        mapper.finishDelivery(user_id,brand_id);
        sqlSession.commit();
        sqlSession.close();
    }
    public void updatePass(int id,String password){
        SqlSession sqlSession=factory.openSession();
        BrandMapper mapper =sqlSession.getMapper(BrandMapper.class);
        mapper.updatePass(id,password);
        sqlSession.commit();
        sqlSession.close();
    }



}
